function foo( n: string| number){
    (n as string).length;
}

function foo1( n: string| number){
    if(typeof n === 'string'){
        n.length
    }
    //(n as string).length;
}

function foo2( n:{username:string} | {age:number}){
    if('username' in n){
        n.username.length
    }
}

class Foo{
    username = 'xiaoming'
}

class Bar{
    age = 18
}

function baz(n: Foo|Bar){
    if(n instanceof Foo){
        n.username.length
    }

}

function baz1(n:'username'| 213){
    if(n === 'username'){
        n.length
    }
}

// is 类型谓词，可以做到类型保护
function isString(n:any):n is string{
    return typeof n === 'string'
}

function fooz( n: string| number){
    if(isString(n)){
        n.length
    }
}